package com.crazyauntzhang.easyshopbackend.controller;

import com.crazyauntzhang.easyshopbackend.domain.User;
import com.crazyauntzhang.easyshopbackend.request.FindPeriodTurnoverRequest;
import com.crazyauntzhang.easyshopbackend.result.FindTurnoverResponse;
import com.crazyauntzhang.easyshopbackend.result.ResponseResult;
import com.crazyauntzhang.easyshopbackend.service.TurnoverFlowService;
import com.crazyauntzhang.easyshopbackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BusinessController {

	private final UserService userService;
	private final TurnoverFlowService turnoverFlowService;

	@Autowired
	public BusinessController(UserService userService, TurnoverFlowService turnoverFlowService) {
		this.userService = userService;
		this.turnoverFlowService = turnoverFlowService;
	}

	/**
	 * 查询当日营业情况
	 *
	 * @return 响应体
	 */
	@PostMapping("/findTodayTurnover")
	public ResponseResult<Object> findTodayTurnover() {
		List<FindTurnoverResponse> todayTurnover = turnoverFlowService.findTodayTurnover();
		if (todayTurnover.isEmpty())
			return new ResponseResult<>(10404, "TodayTurnover Not Found");
		double sumTurnover = 0.0;
		double sumProfit = 0.0;
		for (FindTurnoverResponse findTurnoverResponse : todayTurnover) {
			String userID = findTurnoverResponse.getUserID();
			User user = userService.findUserById(userID, true);
			String username = user.getUsername();
			findTurnoverResponse.setUsername(username);
			sumTurnover += findTurnoverResponse.getTurnover();
			sumProfit += findTurnoverResponse.getProfit();
		}
		Map<String, Object> findTodayTurnoverMap = new HashMap<>();
		findTodayTurnoverMap.put("sumTurnover", sumTurnover);
		findTodayTurnoverMap.put("sumProfit", sumProfit);
		findTodayTurnoverMap.put("detail", todayTurnover);
		return new ResponseResult<>(10200, "OK", findTodayTurnoverMap);
	}

	/**
	 * 查询历史时间段营业情况
	 *
	 * @param findPeriodTurnoverRequest 请求体
	 * @return 响应体
	 */
	@PostMapping("/findPeriodTurnover")
	public ResponseResult<Object> findPeriodTurnover(@RequestBody FindPeriodTurnoverRequest findPeriodTurnoverRequest) {
		LocalDate beginDate = findPeriodTurnoverRequest.getBeginDate();
		LocalDate endDate = findPeriodTurnoverRequest.getEndDate();
		List<FindTurnoverResponse> periodTurnover = turnoverFlowService.findPeriodTurnover(beginDate, endDate);
		if (periodTurnover.isEmpty())
			return new ResponseResult<>(10404, "TodayTurnover Not Found");
		double sumTurnover = 0.0;
		double sumProfit = 0.0;
		for (FindTurnoverResponse findTurnoverResponse : periodTurnover) {
			String userID = findTurnoverResponse.getUserID();
			User user = userService.findUserById(userID, true);
			String username = user.getUsername();
			findTurnoverResponse.setUsername(username);
			sumTurnover += findTurnoverResponse.getTurnover();
			sumProfit += findTurnoverResponse.getProfit();
		}
		Map<String, Object> findPeriodTurnoverMap = new HashMap<>();
		findPeriodTurnoverMap.put("sumTurnover", sumTurnover);
		findPeriodTurnoverMap.put("sumProfit", sumProfit);
		findPeriodTurnoverMap.put("detail", periodTurnover);
		return new ResponseResult<>(10200, "OK", findPeriodTurnoverMap);
	}

}
